package com.itheima.treeset_demo.comparable_demo;

// 2 自定义的Student类实现Comparable接口
public class Student implements Comparable<Student> {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    // 3 重写里面的compareTo​方法
    @Override
    public int compareTo(Student o) {
        // 根据年龄的升序排序
        int result = this.age - o.age; // 10
        // 年龄相同 , 在根据名字排序
        // String  : public int compareTo(String anotherString) 按字典顺序比较两个字符串
        // result = ( result == 0 ? this.name.compareTo(o.name) : result);
        return result == 0 ? this.name.compareTo(o.name) : result;
    }
}
